from django.db import models
from taotao.utils.models import BaseModel
from user.models import User


# Create your models here.
class Goods(BaseModel):
    category_choice = (
        (0, '自行车'),
        (1, '电动车'),
        (2, '书籍教材'),
        (3, '数码电器'),
        (4, '生活用品'),
        (5, '校园代步'),
        (6, '租赁'),
    )
    trader_choice = (
        (0, '线下交易'),
        (1, '线上交易')
    )
    name = models.CharField(max_length=32, verbose_name='商品名')
    price = models.DecimalField(max_digits=8, decimal_places=2, verbose_name='商品价格')
    goods_img = models.ImageField(upload_to='goods', max_length=255, verbose_name='商品图', blank=True, null=True)

    create_user = models.ForeignKey(User, on_delete=models.DO_NOTHING, db_constraint=False, verbose_name='商品创建人')
    category = models.IntegerField(choices=category_choice, verbose_name='商品分类')

    desc = models.CharField(max_length=255, verbose_name='商品描述')
    trader_way = models.IntegerField(choices=trader_choice, default=0, verbose_name='商品交易方式')
    pub_date = models.DateTimeField(auto_now_add=True, verbose_name='上架时间')

    @property
    def goods_create_user(self):
        return self.create_user.username

    def create_user_qq(self):
        return self.create_user.qq
    def create_user_weixin(self):
        return self.create_user.weixin
    def create_user_telephone(self):
        return self.create_user.telephone

    @property
    def goods_category_name(self):
        return self.get_category_display()

    @property
    def trader_way_name(self):
        return self.get_trader_way_display()

    class Meta:
        verbose_name_plural = '商品表'

    def __str__(self):
        return self.name


class GoodsDetail(models.Model):
    trader_choice = (
        (0, '线下交易'),
        (1, '线上交易')
    )
    desc = models.CharField(max_length=255, verbose_name='商品描述')
    trader_way = models.IntegerField(choices=trader_choice, default=0, verbose_name='商品交易方式')
    pub_date = models.DateTimeField(auto_now_add=True, verbose_name='上架时间')

    @property
    def trader_way_name(self):
        return self.get_trader_way_display()

    class Meta:
        verbose_name_plural = '商品详情表'
